import { Cloud } from 'laf-client-sdk'
import { useUserStore } from '@/store/modules/user'
const userStore = useUserStore()
const cloud = new Cloud({
  baseUrl: 'https://su75ks.laf.run', // <APP_ID> 在首页应用列表获取
  getAccessToken: () => userStore.token,
})

//获取党员ID
export async function getAccountID(num) {
  console.log(num)
  const res = await cloud.invoke('curd_op', {
    collection: 'Account_Inf',
    operation: 'get',
    data: {
      where: {
        num: num,
      },
    },
  })
  return res
}

//获取支部ID
export async function getBranchID(num) {
  const res = await cloud.invoke('curd_op', {
    collection: 'Account_Inf',
    operation: 'get',
    data: {
      where: {
        utype: 2,
        num: num,
      },
    },
  })
  return res.data[0]._id
}

//获取支部名称(注意最终可能会改(根据数据库来))
export async function getBranchName(_id) {
  const res = await cloud.invoke('curd_op', {
    collection: 'Account_Inf',
    operation: 'get',
    data: {
      where: {
        _id: _id,
        utype: 2,
      },
    },
  })
  return res.data[0].info.branch_name
}

//获取一个支部所有成员
export async function getPartyMember(_id) {
  const res = await cloud.invoke('getAllBranchMember', { branch_id: _id })
  return res
}

//获取本学院所有党员
export async function getCollegeMember() {
  const res = await cloud.invoke('getAllMember')
  return res
}

export async function getMemberName(_id) {
  const res = await cloud.invoke('curd_op', {
    collection: 'Account_Inf',
    operation: 'get',
    data: {
      where: {
        _id: _id,
        utype: 1,
      },
    },
  })
  return res
}

export async function getCollegeName(_id) {
  const res = await cloud.invoke('curd_op', {
    collection: 'Account_Inf',
    operation: 'get',
    data: {
      where: {
        _id: _id,
        utype: 3,
      },
    },
  })
  return res
}

//获取所有支部数据
export async function getAllBranch() {
  const res = await cloud.invoke('curd_op', {
    collection: 'Account_Inf',
    operation: 'get',
    data: {
      where: {
        utype: 2,
      },
    },
  })
  return res
}
